/**
 * the file is after response page ,draw the driver real-time path on the map  
 */
(function(){
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyAuBWJtxkiuJGQ_IpfGxJJFAnoLSE59DVo&sensor=false&'
			+ 'callback=initializeMap&language=zh-CN&libraries=geometry';
	document.body.appendChild(script);
})(jQuery);

var map, markerPassager, markerDriver, passagerWin, driverWin;
var passagerLatLng, driverLatLng, oldLatLng, setTimeOutFunc, continueTrace = true;
/**
 * initialize map acccording to the given position
 * @param lat
 * @param lng
 */
function initializeMap() {
	alert("passagerLat:" + window.customer.latitude);
	alert("passagerLng:" + window.customer.longitude);
	alert("driverLat:" + window.backDriInfo.driverInfo.lat);
	alert("driverLng:" + window.backDriInfo.driverInfo.lng);
	var position = new google.maps.LatLng(window.customer.latitude, window.customer.longitude);
	var mapOptions = {
    		zoom: 16,
			center: position,
			panControl: false,
			zoomControl: false,
			scaleControl: false,
			streetViewControl: false,
			overviewMapControl: false,
			mapTypeControl: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
	map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
	window.initializeOtherElement(map);
}
/**
 * initialize other elements like, markers, infowindows and so on
 */
window.initializeOtherElement = function(map) {
	initializeDriverInfo();
	initializeListener();
	
	passagerLatLng = new google.maps.LatLng(window.customer.latitude, window.customer.longitude);
	driverLatLng = new google.maps.LatLng(window.backDriInfo.driverInfo.lat, window.backDriInfo.driverInfo.lng);
	markerPassager = initializeMarker(passagerLatLng, map);
	markerDriver = initializeMarker(driverLatLng, map);
	markerPassager.setMap(map);
	markerDriver.setMap(map);
	driverWin = new google.maps.InfoWindow({
	      content: "<p>distance:</p>" + window.backDriInfo.distance + "</p><p>saveTime:" + window.backDriInfo.saveTime + "</p>"
	 }); 
	driverWin.open(map, markerDriver);
	setTimeout(function() {
	      driverWin.close();
	}, 2000);
	setTimeout(function() {
      realTimeMove();
    }, 1000);
};

/**
 * initialize driver info in $("driverInfo")
 */
function initializeDriverInfo() {
	$("#driphoto").attr("src",window.backDriInfo.driverInfo.photo);
	$("#name").text(window.backDriInfo.driverInfo.driverName);
	$("#credit").text(window.backDriInfo.driverInfo.credit);
}

function initializeListener() {
	$("#mapContainer").on('tap', function() {
		driverWin.setContent("<p>distance:" + window.backDriInfo.distance + "</p><p>saveTime:" + window.backDriInfo.saveTime + "</p>");
		driverWin.open(map, markerDriver);
		setTimeout(function() {
		      driverWin.close();
		}, 2000);
	});
}
function realTimeMove() {
	if(continueTrace) {	
//		alert("continue");
		oldLatLng = driverLatLng;
		var invocationData = {
				adapter : 'stAdp',
				procedure : 'getDriverRealTimePosition',
				parameters : [{'driverId' : window.backDriInfo.driverInfo.driverId}]
		};
		WL.Client.invokeProcedure(invocationData, {
			onSuccess : getPositionSuccess,
			onFailure : getPositionFailure
		});
		
	} else {
		alert("cleartimeout");
		clearTimeout(setTimeOutFunc);
	}
}
/**
 * get driver position request success 
 */
function getPositionSuccess(result) {
	var myResult = result.invocationResult;
	var currentLat = myResult.currentLat;
	var currentLng = myResult.currentLng;
	window.backDriInfo.distance = myResult.distance;
	window.backDriInfo.saveTime = myResult.saveTime;
	driverLatLng = new google.maps.LatLng(currentLat, currentLng);
	markerDriver.setPosition(driverLatLng);
//	alert("oldLatLng:::" + oldLatLng.lat() + "   " + oldLatLng.lng());
//	alert("driverLatLng:::" + driverLatLng.lat() + "   " + driverLatLng.lng());
	var points = [oldLatLng, driverLatLng];
	var mypath = new google.maps.Polyline({
		path:points,
		strokeColor:"000000",
		strokeOpacity: 1.0,
		strokeWeight: 1,
		map: map
	});
	var currentDistance = google.maps.geometry.spherical.computeDistanceBetween(window.passengerLatLng, driverLatLng);
	if(currentDistance < 50)
		continueTrace = false;
	setTimeOutFunc = setTimeout("realTimeMove()", 1000);
}
/**
 * get driver position request fail 
 */
function getPositionFailure(result) {
	alert("fail");
	setTimeOutFunc = setTimeout("realTimeMove()", 1000);
}
/**
 * return a Marker on the given map according to the given position
 * @param latlng
 * @param map
 * @returns {google.maps.Marker}
 */

function initializeMarker(latlng, map) {
	marker = new google.maps.Marker({
		map: map,
		visible: true,
		draggable: true,
		animation: google.maps.Animation.DROP,
		position: latlng
	});
	return marker;
}